﻿Imports System.ServiceModel.DomainServices.Client.ApplicationServices

Partial Public Class MainPage
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
    End Sub

#Region "Status Bar"
    Public Enum EnumTipusMissatge
        Informacio
        Warning
        [Error]
    End Enum

    Private tmrStatusBar As System.Threading.Timer = New System.Threading.Timer(AddressOf StatusBarTimerCallback)

    Public Sub ShowStatusBar(ByVal Tipus As EnumTipusMissatge, ByVal Message As String)
        txtStatusBar.Text = Message
        Select Case Tipus
            Case EnumTipusMissatge.Error
                Me.imgStatusBar.SetValue(ToolTipService.ToolTipProperty, StringLibrary.Errorx)
                Me.imgStatusBar.SetValue(Image.SourceProperty, New Media.Imaging.BitmapImage(New Uri("/LullabyFullDeCaixa;component/Imatges/excla.png", UriKind.Relative)))
            Case EnumTipusMissatge.Warning
                Me.imgStatusBar.SetValue(ToolTipService.ToolTipProperty, StringLibrary.Advertencia)
                Me.imgStatusBar.SetValue(Image.SourceProperty, New Media.Imaging.BitmapImage(New Uri("/LullabyFullDeCaixa;component/Imatges/Warning.png", UriKind.Relative)))
            Case Else
                Me.imgStatusBar.SetValue(ToolTipService.ToolTipProperty, StringLibrary.Informacio)
                Me.imgStatusBar.SetValue(Image.SourceProperty, New Media.Imaging.BitmapImage(New Uri("/LullabyFullDeCaixa;component/Imatges/informacio.png", UriKind.Relative)))
        End Select

        StatusBar.Visibility = Windows.Visibility.Visible
        tmrStatusBar.Change(25000, System.Threading.Timeout.Infinite)
    End Sub

    Private Sub StatusBarTimerCallback(ByVal State As Object)
        Deployment.Current.Dispatcher.BeginInvoke(New Action(AddressOf HideStatusBar))
    End Sub

    Private Sub HideStatusBar()
        StatusBar.Visibility = Windows.Visibility.Collapsed
    End Sub
#End Region

    Public Sub Navigate(ByVal Uri As Uri)
        ContentFrame.Navigate(Uri)
    End Sub

    Private Sub ContentFrame_Navigated(ByVal sender As System.Object, ByVal e As System.Windows.Navigation.NavigationEventArgs)
        If DirectCast(RiaContext.Current.Authentication, FormsAuthentication).User Is Nothing OrElse _
           DirectCast(RiaContext.Current.Authentication, FormsAuthentication).User.Identity.IsAuthenticated = False Then
            ContentFrame.Navigate(New Uri("/Login", UriKind.Relative))
        End If
    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
        Me.tmrStatusBar.Dispose()
    End Sub

End Class